package com.tianhui.consignortwofei.manager;

import android.content.Context;
import android.content.SharedPreferences;

import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.utils.ApplicationUtil;

import java.util.Map;


/**
 * @author fgs
 * @date 2020/3/27
 **/
public class SharedPreferenceManager {

    private static final String SHARED_USER_INFO = "userInfo";

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    private SharedPreferenceManager() {
        sharedPreferences = ApplicationUtil.getContext().getSharedPreferences(SHARED_USER_INFO, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }

    public static SharedPreferenceManager getInstance() {
        return SingletonHolder.instance;
    }

    public void setRolePosition(int position) {
        editor.putInt(KeyValue.KEY_ROLE_POSITION, position);
        editor.commit();
    }

    public int getRolePosition() {
        return sharedPreferences.getInt(KeyValue.KEY_ROLE_POSITION, 0);
    }

    private static class SingletonHolder {
        private static SharedPreferenceManager instance = new SharedPreferenceManager();
    }


    public void clear() {
        editor.putString("driverId", "");
        editor.putString(KeyValue.KEY_TOKEN, "");
//        editor.putString("phone", "");
        editor.commit();
    }

    public String getToken() {
        return sharedPreferences.getString(KeyValue.KEY_TOKEN, "");
    }

    public void setToken(String token) {
        editor.putString(KeyValue.KEY_TOKEN, token);
        editor.commit();
    }

    public void clearToken() {
        editor.putString(KeyValue.KEY_TOKEN, "");
        editor.commit();
    }


    /**
     * 货主id
     *
     * @param driverId
     */
    public void setConsignorId(String driverId) {
        editor.putString(KeyValue.KEY_CONSIGNOR_ID, driverId);
        editor.commit();
    }

    /**
     * 货主id
     */
    public String getConsignorId() {
        String driverId = sharedPreferences.getString(KeyValue.KEY_CONSIGNOR_ID, "");
        return driverId;
    }

    /**
     * 角色id
     *
     * @param roleId
     */
    public void setRoleId(String roleId) {
        editor.putString(KeyValue.KEY_ROLE_ID, roleId);
        editor.commit();
    }

    /**
     * 角色id
     */
    public String getRoleId() {
        String driverId = sharedPreferences.getString(KeyValue.KEY_ROLE_ID, "");
        return driverId;
    }

    /**
     * 货主名称
     *
     * @param name
     */
    public void setConsignorName(String name) {
        editor.putString(KeyValue.KEY_CONSIGNOR_NAME, name);
        editor.commit();
    }

    /**
     * 货主名称
     */
    public String getConsignorName() {
        String consignorName = sharedPreferences.getString(KeyValue.KEY_CONSIGNOR_NAME, "");
        return consignorName;
    }

    /**
     * 手机号
     *
     * @param phone
     */
    public void setPhone(String phone) {
        editor.putString(KeyValue.KEY_PHONE, phone);
        editor.commit();
    }

    /**
     * 手机号
     */
    public String getPhone() {
        return sharedPreferences.getString(KeyValue.KEY_PHONE, "");
    }


    /**
     * 账号
     *
     * @param accountNumber
     */
    public void setAccountNumber(String accountNumber) {
        editor.putString(KeyValue.KEY_ACCOUNT_NUMBER, accountNumber);
        editor.commit();
    }

    /**
     * 账号
     */
    public String getAccountNumber() {
        return sharedPreferences.getString(KeyValue.KEY_ACCOUNT_NUMBER, "");
    }

    /**
     * baseUrl
     *
     * @param url
     */
    public void setBaseUrl(String url) {
        editor.putString(KeyValue.KEY_BASE_URL, url);
        editor.commit();
    }

    /**
     * baseUrl
     */
    public String getBaseUrl() {
        return sharedPreferences.getString(KeyValue.KEY_BASE_URL, "");
    }


    /**
     * baseImageUrl
     *
     * @param url
     */
    public void setBaseImageUrl(String url) {
        editor.putString(KeyValue.KEY_BASE_IMAGE_URL, url);
        editor.commit();
    }

    /**
     * baseImageUrl
     */
    public String getBaseImageUrl() {
        return sharedPreferences.getString(KeyValue.KEY_BASE_IMAGE_URL, "");
    }

    public void setVehicleFront(boolean name) {
        editor.putBoolean(KeyValue.KEY_VEHICLE_FRONT, name);
        editor.commit();
    }

    public boolean getVehicleFront() {
        return sharedPreferences.getBoolean(KeyValue.KEY_VEHICLE_FRONT, false);
    }

    /**
     * baseWebUrl
     *
     * @param url
     */
    public void setBaseWebUrl(String url) {
        editor.putString(KeyValue.KEY_BASE_WEB_URL, url);
        editor.commit();
    }

    /**
     * baseWebUrl
     */
    public String getBaseWebUrl() {
        return sharedPreferences.getString(KeyValue.KEY_BASE_WEB_URL, "");
    }


    /**
     * 位置信息
     *
     * @param map
     */
    public void setLocationInfo(Map<String, String> map) {
        if (map == null) return;
        if (map.isEmpty()) {
//            editor.putString(KeyValue.KEY_LATITUDE, "");
//            editor.putString(KeyValue.KEY_LONGITUDE, "");
//            editor.putString(KeyValue.KEY_ADDRESS, "");
//            editor.putString(KeyValue.KEY_ADDRESS_CODE, "");
            return;
        }
        editor.putString(KeyValue.KEY_LATITUDE, map.get(KeyValue.KEY_LATITUDE));
        editor.putString(KeyValue.KEY_LONGITUDE, map.get(KeyValue.KEY_LONGITUDE));
        editor.putString(KeyValue.KEY_ADDRESS, map.get(KeyValue.KEY_ADDRESS));
        editor.putString(KeyValue.KEY_ADDRESS_CODE, map.get(KeyValue.KEY_ADDRESS_CODE));
        editor.commit();
    }

    /**
     * 位置信息编码
     */
    public String getCityCode() {
        return sharedPreferences.getString(KeyValue.KEY_ADDRESS_CODE, "");
    }

    /**
     * city
     */
    public String getCity() {
        return sharedPreferences.getString(KeyValue.KEY_CITY, "");
    }

    /**
     * Address
     */
    public String getAddress() {
        return sharedPreferences.getString(KeyValue.KEY_ADDRESS, "");
    }

    public void setAddressFront(boolean name) {
        editor.putBoolean(KeyValue.KEY_ADRESS_FRONT, name);
        editor.commit();
    }

    public boolean getAddressFront() {
        return sharedPreferences.getBoolean(KeyValue.KEY_ADRESS_FRONT, false);
    }

    /**
     * Latitude
     */
    public String getLatitude() {
        return sharedPreferences.getString(KeyValue.KEY_LATITUDE, "");
    }


    /**
     * Longitude
     */
    public String getLongitude() {
        return sharedPreferences.getString(KeyValue.KEY_LONGITUDE, "");
    }

    //是否开启推送
    public void setPushStatus(boolean isOpen) {
        editor.putBoolean(KeyValue.KEY_PUSH_STATUS, isOpen);
        editor.commit();

    }

    //是否开启推送
    public boolean getPushStatus() {
        return sharedPreferences.getBoolean(KeyValue.KEY_PUSH_STATUS, true);
    }

    //个性化推荐
    public void setPersonalPushStatus(boolean isOpen) {
        editor.putBoolean(KeyValue.KEY_PERSONAL_PUSH_STATUS, isOpen);
        editor.commit();

    }

    //个性化推荐
    public boolean getPersonalPushStatus() {
        return sharedPreferences.getBoolean(KeyValue.KEY_PERSONAL_PUSH_STATUS, false);
    }


    //审核状态
    public void setAuditState(int isAudit) {
        editor.putInt(KeyValue.KEY_AUDIT_STATE, isAudit);
        editor.commit();

    }

    //审核状态
    public int getAuditState() {
        return sharedPreferences.getInt(KeyValue.KEY_AUDIT_STATE, 0);
    }

    //是否记住密码
    public void setRememberPasswordState(boolean isRemember) {
        editor.putBoolean(KeyValue.KEY_REMEMBER_PASSWORD, isRemember);
        editor.commit();
    }

    //是否记住密码
    public boolean getRememberPasswordState() {
        return sharedPreferences.getBoolean(KeyValue.KEY_REMEMBER_PASSWORD, false);
    }

    //密码
    public void setPassword(String password) {
        editor.putString(KeyValue.KEY_PASSWORD, password);
        editor.commit();
    }

    //密码
    public String getPassword() {
        return sharedPreferences.getString(KeyValue.KEY_PASSWORD, "");
    }

    //判断是不是间隔一天
    public void setTongZhiFirst(String position) {
        editor.putString(KeyValue.KEY_TONGZHI_FIRST, position);
        editor.commit();
    }

    public String getTongZhiFirst() {
        return sharedPreferences.getString(KeyValue.KEY_TONGZHI_FIRST, "");
    }


    //判断是不是间隔几小时
    public void setShiJianFirst(String position) {
        editor.putString(KeyValue.KEY_SHIJIAN_FIRST, position);
        editor.commit();
    }

    public String getShiJianFirst() {
        return sharedPreferences.getString(KeyValue.KEY_SHIJIAN_FIRST, "");
    }

    //判断是不是间隔几小时（回款）
    public void setShiJianFirstHuiKuan(String position) {
        editor.putString(KeyValue.KEY_SHIJIAN_FIRST_HUIKUAN, position);
        editor.commit();
    }

    public String getShiJianFirstHuiKuan() {
        return sharedPreferences.getString(KeyValue.KEY_SHIJIAN_FIRST_HUIKUAN, "");
    }



}
